from django.shortcuts import render
from django.views.generic import TemplateView
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.template.context_processors import csrf
from django.contrib import messages
from home.context_processors import hasGroup
from django.contrib.auth.models import User,Group
from profiles.models import Patient
from string import digits



# Create your views here.
def login(request):
    if request.user.is_authenticated:
        messages.add_message(request, messages.INFO, 'You are already Logged in.')
        return HttpResponseRedirect('/home')
    else:
        c = {}
        c.update(csrf(request))
        return render(request, 'loginmodule/login.html', c)


def auth_view(request):
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        messages.add_message(request, messages.INFO, 'Your are now Logged in.')
        return HttpResponseRedirect('/home')
    else:
        messages.add_message(request, messages.WARNING, 'Invalid Login Credentials.')
        return HttpResponseRedirect('/login')


def logout(request):
    if request.user.is_authenticated:
        auth.logout(request)
    messages.add_message(request, messages.INFO, 'You are Successfully Logged Out')
    messages.add_message(request, messages.INFO, 'Thanks for visiting.')
    return HttpResponseRedirect('/login')

def pre_Register(request):
    try:
        return render(request, 'loginmodule/register_pa.html')
    except:
        messages.add_message(request, messages.WARNING, 'Network Error,Please try it again.')
        return HttpResponseRedirect('/home')

def pa_Register(request):
    if request.user:
        username = request.POST.get('username')
        if User.objects.filter(username=username).exists():
            messages.add_message(request, messages.ERROR, 'Username Already Exists.')
            return HttpResponseRedirect('/loginmodule/pre_register')
        password = request.POST.get('password1')

        cpassword = request.POST.get('password2')

        if not password == cpassword:
            messages.add_message(request, messages.ERROR, 'Passwords not matching.')
            return HttpResponseRedirect('/loginmodule/pre_register')
        first_name = request.POST.get('first_name')
        last_name = request.POST.get('last_name')
        contact_no = request.POST.get('contact_no')
        if not contact_no.isdigit():
            messages.add_message(request, messages.ERROR, 'Wrong Contact no.')
            return HttpResponseRedirect('/loginmodule/pre_register')
        address = request.POST.get('address')
        dob = request.POST.get('dob')
        blood_group = request.POST.get('blood_group')
        email = request.POST.get('email')
        identity_no = request.POST.get("identity")

        remove_digits = str.maketrans('', '', digits)
        res = identity_no.translate(remove_digits)

        if len(identity_no) != 18 or res != '':
            messages.add_message(request, messages.WARNING, 'Id number is not in format')
            return HttpResponseRedirect('/loginmodule/pre_register')

        for pa in Patient.objects.all():
            if pa.patient_id == identity_no:
                messages.add_message(request, messages.WARNING, 'Id number has been registered')
                return HttpResponseRedirect('/loginmodule/pre_register')

        patient = User.objects.create_user(username=username, password=password, first_name=first_name, last_name=last_name, email=email)
        patient.patient = Patient(patient_id = identity_no, contact_no=int(contact_no), address=address, dob=dob, blood_group=blood_group)
        patient.patient.save()
        patient.save()
        try:
            group = Group.objects.get(name='patient')

        except:
            group = Group()
            group.name = 'patient'
            group.save()

        group.user_set.add(patient)
        group.save()
        print(group.user_set.all())

        messages.add_message(request, messages.WARNING, 'Successfully Registered '+username)
        return HttpResponseRedirect('/login')
    else:
        messages.add_message(request, messages.WARNING, 'Access Denied.')
        return HttpResponseRedirect('/home')
